package com.stylesmile.socket.util;

import com.stylesmile.socket.http.Request;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static com.stylesmile.socket.util.FastUtil.split;

public class RequestUtil {
    public static void readRequestLine(Request request, byte[] bytes) throws IOException {
        String line = new String(bytes);

        String[] tokens = split(line, " ", -1);
        if (tokens.length != 3)
            throw new IOException("invalid request line: \"" + line + "\"");
        try {
            String method = tokens[0];
            // must remove '//' prefix which constructor parses as host name
            URI uri = new URI(tokens[1]); //todo: by noear 20220707 new URI(trimDuplicates(tokens[1], '/'));
            String version = tokens[2]; // RFC2616#2.1: allow implied LWS; RFC7230#3.1.1: disallow it
            System.out.println(version);
        } catch (URISyntaxException use) {
            use.printStackTrace();
//            throw new IOException("invalid URI: " + use.getMessage());
        }
    }
}
